<template>
  <div id="notice">
    <div class="box">
      <div class="top">
        新消息
        <span @click="aaaaaaaa">忽略全部</span>
      </div>
      <div class="notice">
        <!-- <user-item :msgnum="item.num" :onclicks="startMsg" :user="item.user" v-for="(item, index) in notice" :keys="index" :key="index"></user-item> -->
        <noticeMsg :user="item.user" :newmsgnum="item.new_msg_num" @click.native="startMsg(item, index)" :index="index" v-for="(item, index) in notice" :key="index"/>
      </div>
    </div>
  </div>
</template>
<script>
import noticeMsg from '../../components/noticeMsg'

var inWin
var inScreen
export default {
  data: () => {
    return {
      notice: [
      ]
    }
  },
  components: {
    noticeMsg
  },
  watch: {
    notice: function () {
      if (this.notice.length === 0) {
        console.log('1111')
        this.$electron.ipcRenderer.send('notice_hide')
      }
    }
  },
  created () {
    window.Notice = this
    var that = this
    inScreen = this.$electron.screen.getPrimaryDisplay().workAreaSize
    inWin = this.$electron.remote.getCurrentWindow()
    this.winReSize()
    this.$electron.ipcRenderer.on('main_click_userchat', function (event, data) {
      console.log('soudaclick')
      that.notice.forEach(function (item, index) {
        if (item.user.user_id === data) {
          that.notice.splice(index, 1)
          that.descArr()
          that.winReSize()
          that.$forceUpdate()
        }
      })
    })
    this.$electron.ipcRenderer.on('new_msg', function (event, data) {
      data = JSON.parse(JSON.stringify(data))
      data.last_butt_time = data.user.last_butt_time
      var innotice = null
      that.notice.forEach(function (item, index) {
        if (item.user.user_id === data.user.user_id) {
          innotice = index
        }
      })
      if (innotice !== null) {
        that.notice[innotice].time = data.last_butt_time
        that.notice[innotice].new_msg_num += 1
      } else {
        data.new_msg_num = 1
        that.notice.push(data)
      }
      that.descArr()
      that.winReSize()
      that.$forceUpdate()
    })
  },
  methods: {
    descArr () {
      this.notice.sort(function (o1, o2) {
        return o1.time - o2.time
      }).reverse()
    },
    startMsg (item, index) {
      this.$electron.ipcRenderer.send('main_show', item)
      this.notice.splice(index, 1)
      this.winReSize()
    },
    aaaaaaaa () {
      this.notice = []
      this.$electron.ipcRenderer.send('notice_hide')
    },
    winReSize () {
      let width = 230
      if (process.env.NODE_ENV === 'development') width += 600
      var addHeight = (this.notice.length - 1) * 40 > 320 ? 320 : (this.notice.length - 1) * 40
      addHeight = addHeight < 0 ? 0 : addHeight
      inWin.setContentSize(width, 90 + addHeight)
      inWin.setPosition(inScreen.width - width, inScreen.height - (90 + addHeight))
    }
  }
}
</script>

<style  lang="less" scoped>
@import './style.less';
</style>
